package com.qihang.common;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;

public class PagerModel {

	private List result; // 结果集
	private int pageSize; // 页大小
	private int startPage; // 起始页 从1开始
	private ScrollableResults scrollableResults;
	private int totalResults; // 总记录的条数
	private int totalPages; // 总页数
	private int resultsStart;//开始位置
	private int resultsEnd;//结束位置
	private String message;
	
	public PagerModel(int resultsStart, int pageSize, Query query) {
		
		
//		this.startPage = startPage;
		this.pageSize = pageSize;
		this.result = null;
		this.resultsStart = resultsStart;
		this.startPage = resultsStart/this.pageSize +1;
		
		
		try {
			this.scrollableResults = query.scroll();
			this.scrollableResults.last();
			if (scrollableResults.getRowNumber() >= 0) {
				this.totalResults = this.scrollableResults.getRowNumber() + 1;
			} else {
				this.totalResults = 0;
			}
			setTotalPages();
			result = query
					.setFirstResult((this.getStartPage() - 1) * this.pageSize)
					.setMaxResults(this.pageSize).list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		
	}
	
	 
//    private int currentPage;  // 当前页(currentPage)  

//	public PagerModel(int startPage, int pageSize, Query query) {
//		this.startPage = startPage;
//		this.pageSize = pageSize;
//		this.result = null;
//		try {
//			this.scrollableResults = query.scroll();
//			this.scrollableResults.last();
//			if (scrollableResults.getRowNumber() >= 0) {
//				this.totalResults = this.scrollableResults.getRowNumber() + 1;
//			} else {
//				this.totalResults = 0;
//			}
//			setTotalPages();
//			result = query
//					.setFirstResult((this.getStartPage() - 1) * this.pageSize)
//					.setMaxResults(this.pageSize).list();
//		} catch (HibernateException e) {
//			e.printStackTrace();
//		}
//	}
	
	


//	public int getStart(){
//		return (this.getStartPage() - 1) * this.pageSize+1;
//	}
//	
//	public int getEnd(){
//		return (this.getStartPage()) * this.pageSize;
//	}
	
	/**
	 * 得到查询结果
	 * 
	 * @return 查询结果
	 */
	public List getResult() {
		return result;
	}

	public int getResultsEnd() {
		return this.resultsEnd = 
				(this.totalResults-this.resultsStart)>this.pageSize?(this.resultsStart+this.pageSize):this.totalResults;
	}

	public void setResultsEnd() {
		this.resultsEnd = (this.totalResults-this.resultsStart)==this.pageSize?(this.totalResults-this.resultsStart):this.totalResults;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		if(this.totalResults == 0){
			return "没有数据 ";
		}else{
			return "显示 "+(this.getResultsStart()+1)+" 到 "+this.getResultsEnd()+" 项，共 "+this.totalResults+" 项";
		}
	}


	public int getResultsStart() {
		return resultsStart;
	}


	public void setResultsStart(int resultsStart) {
		this.resultsStart = resultsStart;
	}


	/**
	 * 得到起始页
	 * 
	 * @return
	 */
	public int getStartPage() {
		if (startPage < 1) {
			startPage = 1;
		}
		if (startPage > totalPages) {
			startPage = totalPages;
		}
		return startPage;
	}

	/**
	 * 得到记录总数
	 * 
	 * @return
	 */
	public int getTotalResults() {
		return totalResults;
	}

	/**
	 * 得到页大小
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 判断是否是第一页
	 * 
	 * @return
	 */
	public boolean isFirstPage() {
		return this.startPage == 1;
	}

	/**
	 * 判断是否是有后一页
	 * 
	 * @return
	 */
	public boolean hasNextPage() {
		return this.startPage < this.totalPages;
	}

	/**
	 * 判断是否是有前一页
	 * 
	 * @return
	 */
	public boolean hasPreviousPage() {
		return this.startPage > 1;
	}

	/**
	 * 设置总页数
	 * 
	 */
	private void setTotalPages() {
		this.totalPages = this.totalResults / this.pageSize;
		if (totalPages * pageSize < totalResults) {
			totalPages++;
		}
	}

	/**
	 * 得到总页数
	 * 
	 * @return
	 */
	public int getTotalPages() {
		return totalPages;
	}

	@Override
	public String toString() {
		return "Page [result=" + result + ", pageSize=" + pageSize
				+ ", startPage=" + startPage + ", scrollableResults="
				+ scrollableResults + ", totalResults=" + totalResults
				+ ", totalPages=" + totalPages + "]";
	}

	
}
